package com.maple.admin.misc.menu.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.maple.admin.misc.menu.dto.MenuDTO;
import com.maple.admin.misc.menu.po.MenuPO;
import com.maple.admin.misc.menu.service.IMenuService;
import com.maple.admin.misc.paging.dto.PagingParamsDTO;
import com.maple.admin.misc.paging.dto.PagingResultDTO;
import com.maple.admin.misc.paging.service.IPagingService;
import com.maple.admin.user.dto.UserDTO;

@Controller
@RequestMapping("/menu")
public class MenuController {
	
	@Autowired
	IMenuService menuService;
	
	@Autowired
    IPagingService pagingService;
	
	/**
	 * load dynamic menus with permission
	 * @param dto
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/data/json", method=RequestMethod.GET)
	@ResponseBody
	public List<Object> myLoadMenus(UserDTO dto, HttpServletRequest request, HttpServletResponse response){
		List<Object> menuList = new ArrayList<Object>();
		menuList = menuService.queryAllMenus();
		return menuList;
		
	}
	
	/**
	 * menu edit page
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public ModelAndView myEditMenusPage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/menu/edit-menu");
		return mv;
	}
	
	/**
	 * menu view page
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/data/tables", method=RequestMethod.GET)
	public ModelAndView myViewMenusPage(HttpServletRequest request, HttpServletResponse response){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/menu/view-menu");
		return mv;
	}
	
	/**
	 * query paginated users 
	 * @param paging
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value="/data/tables/paging", method=RequestMethod.POST)
	@ResponseBody
	public PagingResultDTO myQueryPaginatedMenus(PagingParamsDTO paging, HttpServletRequest request, HttpServletResponse response){
		List<MenuDTO> dataList = menuService.queryPaginatedMenus(paging);
		PagingResultDTO result = new PagingResultDTO();
		// data tables params
		result.setDraw(paging.getDraw());
		result.setRecordsFiltered(pagingService.countTableRows(MenuPO.class));
		result.setRecordsTotal(pagingService.countTableRows(MenuPO.class));
		result.setData(dataList);
		return result;
		
	}
	
	
	
}
